<?php

class Administration_Model_Mapper_Locations extends Jnd_Model_Mapper_Abstract
{
    protected $_dict = array(
        'id' => 'idlocations', 
        'name' => 'name', 
        'address1' => 'address1', 
        'address2' => 'address2', 
        'zipcode' => 'zipcode', 
        'city' => 'city', 
        'country' => 'country', 
        'creationUser' => 'creationuser', 
        'creationDate' => 'creationdate'
    );
    
    protected $_dictDate = array(
        'creationDate' => 'creationdate'
    );
    protected $_cache = "locations";


    /**
     * @param unknown_type $id
     * @param unknown_type $objetModel
     * @return Ambigous <mixed, string, multitype:, unknown>
     */
    public function chercher( $id, $objetModel = null )
    {
        parent::chercher($id, $objetModel);
        foreach ($this->_dictNumeric as $champModel => $champStockage) {
            $methodeSet = "set" . ucfirst($champModel);
            $objetModel->$methodeSet($objetModel->traitementNumerique($champModel));
        }
        foreach ($this->_dictDate as $champModel => $champStockage) {
            $methodeGet = "get" . ucfirst($champModel);
            $methodeSet = "set" . ucfirst($champModel);
            $dateText = str_replace(array(
                " ", 
                "-", 
                ":"
            ), array(
                "/", 
                "/", 
                "/"
            ), $objetModel->$methodeGet());
            if (strlen($dateText) > 8) {
                list ($y, $m, $d, $h, $min, $s) = split('[/]', $dateText);
                $format = Jnd_Global_Constantes::FORMAT_DATE_WEB_LONG;
            } else {
                list ($y, $m, $d) = split('[/.-]', $dateText);
                $h = 0;
                $min = 0;
                $s = 0;
                $format = Jnd_Global_Constantes::FORMAT_DATE_WEB_COURT;
            }
            $dateFormate = date($format, mktime(intval($h), intval($min), intval($s), intval($m), intval($d), intval($y)));
            $objetModel->$methodeSet($dateFormate);
        }
    }

    /**
     * Methode permettant de générer le Table_Select pour le datagrid
     * @see Jnd_Model_Mapper_Abstract::getListSelect()
     */
    public function getListSelect( $values, $environnement, $identite, $options = null )
    {
        $table = $this->_getTable();
        $tableName = $table->info(Zend_Db_Table_Abstract::NAME);
        $dba = $table->getAdapter();
        $select = $dba->select()->from($tableName, $this->_dict, $table->info(Zend_Db_Table_Abstract::SCHEMA));
        
        $champ = 'name';
        if (isset($values[$champ]) && $values[$champ]) {
            $select->where($dba->quoteIdentifier($tableName . '.' . $this->_dict[$champ]) . ' LIKE ?', '%' . $values[$champ] . '%');
        }
        $champ = 'creationUser';
        if (isset($values[$champ]) && $values[$champ]) {
            $select->where($dba->quoteIdentifier($tableName . '.' . $this->_dict[$champ]) . ' LIKE ?', $values[$champ] . '%');
        }
        $champ = 'creationDate';
        if (isset($values[$champ]) && $values[$champ]) {
            $dt = new Jnd_Date($values[$champ], null, 'fr_FR');
            $select->where('DATE(' . $dba->quoteIdentifier($tableName . '.' . $this->_dict[$champ]) . ') = ?', $dt->getDbDate());
        }
        //      echo $select;
        //        die("Select : $select");
        return $select;
    }

}
